<?php
class POG_Base
{

    function __call($method, $argv)
    {
	include_once($GLOBALS['configuration']['plugins_path']."/IPlugin.php");
	include_once($GLOBALS['configuration']['plugins_path']."/plugin.".strtolower($method).".php");
	eval('$plugin = new $method($this,$argv);');
	return $plugin->Execute();
    }

    private function POG_Base()
    {
    }

    function SetFieldAttribute($fieldName, $attributeName, $attributeValue)
    {
       if (isset($this->pog_attribute_type[$fieldName]) && isset($this->pog_attribute_type[$fieldName][$attributeName]))
        {
            $this->pog_attribute_type[$fieldName][$attributeName] = $attributeValue;
        }
    }

    function GetFieldAttribute($fieldName, $attributeName)
    {
        if (isset($this->pog_attribute_type[$fieldName]) && isset($this->pog_attribute_type[$fieldName][$attributeName]))
        {
            return $this->pog_attribute_type[$fieldName][$attributeName];
        }
        return null;
    }

    public function Escape($text)
    {
	if ($GLOBALS['configuration']['db_encoding'] && !is_numeric($text))
	{
            return base64_encode($text);
	}
            return addslashes($text);
    }

    public function Unescape($text)
    {
	if ($GLOBALS['configuration']['db_encoding'] && !is_numeric($text))
	{
            return base64_decode($text);
	}
	return stripcslashes($text);
    }

    protected function FetchObjects($query, $objectClass, $lazy = true)
    {
	$databaseConnection = Database::Connect();
	$result = Database::Reader($query, $databaseConnection);
	$objectList = $this->CreateObjects($result, $objectClass, $lazy);
	return $objectList;
    }

    private function CreateObjects($mysql_result, $objectClass, $lazyLoad = true)
    {
	$objectList = array();
	if ($mysql_result != null)
        {
            while ($row =  Database::Read($mysql_result))
            {
                $pog_object = new $objectClass();
                $this->PopulateObjectAttributes($row, $pog_object);
                $objectList[] = $pog_object;
            }
	}
        return $objectList;
    }

    private function PopulateObjectAttributes($fetched_row, $pog_object)
    {
	$att = $this->GetAttributes($pog_object);
	foreach ($att as $column)
	{
            $pog_object->{$column} = $this->Unescape($fetched_row[strtolower($column)]);
	}
	return $pog_object;
    }

    public function GetAttributes($object, $type='')
    {
	$columns = array();
	foreach ($object->pog_attribute_type as $att => $properties)
	{
            if ($properties['db_attributes'][0] != 'OBJECT')
            {
		if (($type != '' && strtolower($type) == strtolower($properties['db_attributes'][0])) || $type == '')
                {
                    $columns[] = $att;
		}
            }
        }
	return $columns;
    }

    public static function IsColumn($value)
    {
	if (strlen($value) > 2)
	{
            if (substr($value, 0, 1) == '`' && substr($value, strlen($value) - 1, 1) == '`')
            {
		return true;
            }
            return false;
	}
	return false;
    }
}
?>